package com.wanxing.sellerapp.emqx.handlers

import android.annotation.SuppressLint
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import com.alibaba.fastjson.JSONObject
import com.blankj.utilcode.util.ThreadUtils
import com.blankj.utilcode.util.Utils
import com.wanxing.sellerapp.App
import com.wanxing.sellerapp.emqx.MyMqttClient
import com.wanxing.sellerapp.emqx.entity.EmqPayload
import com.wanxing.sellerapp.emqx.interfaces.IMessenger
import com.wanxing.sellerapp.seller.SellerManager

class UpgradeHandler(messenger: IMessenger, sellerManager: SellerManager) : AbstractActionHandler(messenger, sellerManager) {

    @SuppressLint("SdCardPath", "UnspecifiedImmutableFlag")
    override fun handleAction(messageId: String?, data: JSONObject, payload: EmqPayload) {
        ThreadUtils.runOnUiThreadDelayed({
            val intent = Utils.getApp().packageManager.getLaunchIntentForPackage("com.wanxing.sellerapp")
            val restartIntent = PendingIntent.getActivity(
                Utils.getApp(), 0, intent, PendingIntent.FLAG_ONE_SHOT
            )
            val alarmManager = Utils.getApp().getSystemService(Context.ALARM_SERVICE) as AlarmManager
            alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 6000, restartIntent)
//            App.smdt?.smdtSilentInstall("/storage/emulated/0/Download/test.apk", Utils.getApp())
        },100)
    }

}